﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using DC2010;
using System.Timers;
using System.Diagnostics;
using DC2010.Objects.Base;
using DC2010.Objects;
using System.Xml.Serialization;
using RolePlaying.Events;
using log4net;
using System.Reflection;

namespace RolePlaying
{
    public class RM
    {
        private static Dictionary<string, ActiveRectangle> contentRectangles = new Dictionary<string, ActiveRectangle>();

        public static ActiveRectangle Neck { get; set; }
        public static ActiveRectangle RightHand { get; set; }
        public static ActiveRectangle LeftHand { get; set; }
        public static ActiveRectangle Head { get; set; }
        public static ActiveRectangle Body { get; set; }
        public static ActiveRectangle Legs { get; set; }
        public static ActiveRectangle Feet { get; set; }

        public static ActiveRectangle BeltUp { get; set; }
        public static ActiveRectangle BeltDown { get; set; }

        public static ActiveRectangle QuiverA { get; set; }
        public static ActiveRectangle QuiverB { get; set; }
        public static ActiveRectangle QuiverC { get; set; }
        public static ActiveRectangle QuiverD { get; set; }
        public static ActiveRectangle QuiverE { get; set; }
        public static ActiveRectangle QuiverF { get; set; }

        public static void Init()
        {
            //this will be moved to XML and loaded standard way
            AddRectangle("", "Background", new Rectangle(0, 0, 1280, 800));

            AddRectangle("", "PlayerPanel1", new Rectangle(10, 10, 132, 58));
            AddRectangle("", "PlayerPanel2", new Rectangle(10 + 1 * 140, 10, 132, 58));
            AddRectangle("", "PlayerPanel3", new Rectangle(10 + 2 * 140, 10, 132, 58));
            AddRectangle("", "PlayerPanel4", new Rectangle(10 + 3 * 140, 10, 132, 58));

            AddRectangle("", "PlayerPanel1_Hand_Right", new Rectangle(8, 20, 32, 32), Get("PlayerPanel1").rect);
            AddRectangle("", "PlayerPanel1_Hand_Left", new Rectangle(51, 20, 32, 32), Get("PlayerPanel1").rect);
            AddRectangle("", "PlayerPanel1Stats", new Rectangle(90, 5, 35, 50), Get("PlayerPanel1").rect);

            AddRectangle("", "PlayerPanel2_Hand_Right", new Rectangle(8, 20, 32, 32), Get("PlayerPanel2").rect);
            AddRectangle("", "PlayerPanel2_Hand_Left", new Rectangle(51, 20, 32, 32), Get("PlayerPanel2").rect);
            AddRectangle("", "PlayerPanel2Stats", new Rectangle(90, 5, 35, 50), Get("PlayerPanel2").rect);

            AddRectangle("", "PlayerPanel3_Hand_Right", new Rectangle(8, 20, 32, 32), Get("PlayerPanel3").rect);
            AddRectangle("", "PlayerPanel3_Hand_Left", new Rectangle(51, 20, 32, 32), Get("PlayerPanel3").rect);
            AddRectangle("", "PlayerPanel3Stats", new Rectangle(90, 5, 35, 50), Get("PlayerPanel3").rect);

            AddRectangle("", "PlayerPanel4_Hand_Right", new Rectangle(8, 20, 32, 32), Get("PlayerPanel4").rect);
            AddRectangle("", "PlayerPanel4_Hand_Left", new Rectangle(51, 20, 32, 32), Get("PlayerPanel4").rect);
            AddRectangle("", "PlayerPanel4Stats", new Rectangle(90, 5, 35, 50), Get("PlayerPanel4").rect);

            //inventory
            AddRectangle("Inventory", "Inventory", new Rectangle(70, 140, 450, 328));

            Neck = AddRectangle("Inventory", "Inventory_Neck", new Rectangle(10, 64, 36, 36), Get("Inventory").rect);
            RightHand = AddRectangle("Inventory", "Inventory_Right_Hand", new Rectangle(10, 104, 36, 36), Get("Inventory").rect);
            LeftHand = AddRectangle("Inventory", "Inventory_Left_Hand", new Rectangle(122, 104, 36, 36), Get("Inventory").rect);
            Head = AddRectangle("Inventory", "Inventory_Head", new Rectangle(66, 50, 36, 36), Get("Inventory").rect);
            Body = AddRectangle("Inventory", "Inventory_Body", new Rectangle(66, 90, 36, 36), Get("Inventory").rect);
            Legs = AddRectangle("Inventory", "Inventory_Legs", new Rectangle(66, 129, 36, 36), Get("Inventory").rect);
            Feet = AddRectangle("Inventory", "Inventory_Feet", new Rectangle(66, 170, 36, 36), Get("Inventory").rect);

            BeltUp = AddRectangle("Inventory", "Inventory_Belt_Up", new Rectangle(10, 165, 36, 36), Get("Inventory").rect);
            BeltDown = AddRectangle("Inventory", "Inventory_Belt_Down", new Rectangle(10, 199, 36, 36), Get("Inventory").rect);

            QuiverA = AddRectangle("Inventory", "Inventory_Quiver_A", new Rectangle(122, 165, 36, 36), Get("Inventory").rect);
            QuiverB = AddRectangle("Inventory", "Inventory_Quiver_B", new Rectangle(156, 165, 36, 36), Get("Inventory").rect);
            QuiverC = AddRectangle("Inventory", "Inventory_Quiver_C", new Rectangle(122, 199, 36, 36), Get("Inventory").rect);
            QuiverD = AddRectangle("Inventory", "Inventory_Quiver_D", new Rectangle(156, 199, 36, 36), Get("Inventory").rect);
            QuiverE = AddRectangle("Inventory", "Inventory_Quiver_E", new Rectangle(122, 233, 36, 36), Get("Inventory").rect);
            QuiverF = AddRectangle("Inventory", "Inventory_Quiver_F", new Rectangle(156, 233, 36, 36), Get("Inventory").rect);

            AddRectangle("Inventory", "Inventory_0", new Rectangle(164, 30, 36, 36), Get("Inventory").rect);
            AddRectangle("Inventory", "Inventory_1", new Rectangle(198, 30, 36, 36), Get("Inventory").rect);
            AddRectangle("Inventory", "Inventory_2", new Rectangle(232, 30, 36, 36), Get("Inventory").rect);
            AddRectangle("Inventory", "Inventory_3", new Rectangle(266, 30, 36, 36), Get("Inventory").rect);
            AddRectangle("Inventory", "Inventory_4", new Rectangle(300, 30, 36, 36), Get("Inventory").rect);
            AddRectangle("Inventory", "Inventory_5", new Rectangle(334, 30, 36, 36), Get("Inventory").rect);
            AddRectangle("Inventory", "Inventory_6", new Rectangle(368, 30, 36, 36), Get("Inventory").rect);
            AddRectangle("Inventory", "Inventory_7", new Rectangle(402, 30, 36, 36), Get("Inventory").rect);

            AddRectangle("Inventory", "Inventory_8", new Rectangle(164, 64, 36, 36), Get("Inventory").rect);
            AddRectangle("Inventory", "Inventory_9", new Rectangle(198, 64, 36, 36), Get("Inventory").rect);
            AddRectangle("Inventory", "Inventory_10", new Rectangle(232, 64, 36, 36), Get("Inventory").rect);
            AddRectangle("Inventory", "Inventory_11", new Rectangle(266, 64, 36, 36), Get("Inventory").rect);
            AddRectangle("Inventory", "Inventory_12", new Rectangle(300, 64, 36, 36), Get("Inventory").rect);
            AddRectangle("Inventory", "Inventory_13", new Rectangle(334, 64, 36, 36), Get("Inventory").rect);
            AddRectangle("Inventory", "Inventory_14", new Rectangle(368, 64, 36, 36), Get("Inventory").rect);
            AddRectangle("Inventory", "Inventory_15", new Rectangle(402, 64, 36, 36), Get("Inventory").rect);

            AddRectangle("Inventory", "Inventory_Eye", new Rectangle(18, 21, 42, 40), Get("Inventory").rect);
            AddRectangle("Inventory", "Inventory_Mouth", new Rectangle(106, 21, 42, 40), Get("Inventory").rect);

            AddRectangle("Inventory", "Inventory_Treasure", new Rectangle(156, 104, 282, 180), Get("Inventory").rect);
            AddRectangle("Inventory", "Inventory_Scroll", new Rectangle(156, 104, 282, 180), Get("Inventory").rect);

            AddRectangle("Inventory", "Inventory_Container_0", new Rectangle(70, 101, 36, 36), Get("Inventory_Treasure").rect);
            AddRectangle("Inventory", "Inventory_Container_1", new Rectangle(103, 101, 36, 36), Get("Inventory_Treasure").rect);
            AddRectangle("Inventory", "Inventory_Container_2", new Rectangle(137, 101, 36, 36), Get("Inventory_Treasure").rect);
            AddRectangle("Inventory", "Inventory_Container_3", new Rectangle(171, 101, 36, 36), Get("Inventory_Treasure").rect);
            AddRectangle("Inventory", "Inventory_Container_4", new Rectangle(206, 101, 36, 36), Get("Inventory_Treasure").rect);
            AddRectangle("Inventory", "Inventory_Container_5", new Rectangle(239, 101, 36, 36), Get("Inventory_Treasure").rect);
            AddRectangle("Inventory", "Inventory_Container_6", new Rectangle(70, 136, 36, 36), Get("Inventory_Treasure").rect);
            AddRectangle("Inventory", "Inventory_Container_7", new Rectangle(103, 136, 36, 36), Get("Inventory_Treasure").rect);
            AddRectangle("Inventory", "Inventory_Container_8", new Rectangle(137, 136, 36, 36), Get("Inventory_Treasure").rect);
            AddRectangle("Inventory", "Inventory_Container_9", new Rectangle(171, 136, 36, 36), Get("Inventory_Treasure").rect);
            AddRectangle("Inventory", "Inventory_Container_10", new Rectangle(206, 136, 36, 36), Get("Inventory_Treasure").rect);
            AddRectangle("Inventory", "Inventory_Container_11", new Rectangle(239, 136, 36, 36), Get("Inventory_Treasure").rect);

            AddRectangle("", "ActionPanel", new Rectangle(10, 80, 174, 100));

            AddRectangle("", "ActionPanel_11", new Rectangle(0, 280, 40, 50), Get("ActionPanel").rect);
            AddRectangle("", "ActionPanel_12", new Rectangle(0 + 45, 280, 40, 50), Get("ActionPanel").rect);

            AddRectangle("", "ActionPanel_21", new Rectangle(0 + 2 * 45, 280, 40, 50), Get("ActionPanel").rect);
            AddRectangle("", "ActionPanel_22", new Rectangle(0 + 3 * 45, 280, 40, 50), Get("ActionPanel").rect);

            AddRectangle("", "ActionPanel_31", new Rectangle(0, 335, 40, 50), Get("ActionPanel").rect);
            AddRectangle("", "ActionPanel_32", new Rectangle(0 + 45, 335, 40, 50), Get("ActionPanel").rect);

            AddRectangle("", "ActionPanel_41", new Rectangle(0 + 2 * 45, 335, 40, 50), Get("ActionPanel").rect);
            AddRectangle("", "ActionPanel_42", new Rectangle(0 + 3 * 45, 335, 40, 50), Get("ActionPanel").rect);

            AddRectangle("", "WalkingPad", new Rectangle(540, 430, 182, 128));

            AddRectangle("", "Action_0", new Rectangle(10, 200, 174, 22), Get("ActionPanel").rect);
            AddRectangle("", "Action_1", new Rectangle(10, 220, 174, 22), Get("ActionPanel").rect);
            AddRectangle("", "Action_2", new Rectangle(10, 244, 174, 22), Get("ActionPanel").rect);
            AddRectangle("", "Action_3", new Rectangle(10, 266, 174, 22), Get("ActionPanel").rect);

            AddRectangle("", "SpellSheet", new Rectangle(896 - 174 + 10, 10, 174, 66));

            //Member 1 selected
            AddRectangle("", "SpellSheet_Switch_12", new Rectangle(94, 0, 24, 14), Get("SpellSheet").rect);
            AddRectangle("", "SpellSheet_Switch_13", new Rectangle(122, 0, 24, 14), Get("SpellSheet").rect);
            AddRectangle("", "SpellSheet_Switch_14", new Rectangle(150, 0, 24, 14), Get("SpellSheet").rect);

            //Member 2 selected
            AddRectangle("", "SpellSheet_Switch_21", new Rectangle(94, 0, 24, 14), Get("SpellSheet").rect);
            AddRectangle("", "SpellSheet_Switch_23", new Rectangle(122, 0, 24, 14), Get("SpellSheet").rect);
            AddRectangle("", "SpellSheet_Switch_24", new Rectangle(150, 0, 24, 14), Get("SpellSheet").rect);

            //Member 3 selected
            AddRectangle("", "SpellSheet_Switch_31", new Rectangle(94, 0, 24, 14), Get("SpellSheet").rect);
            AddRectangle("", "SpellSheet_Switch_32", new Rectangle(122, 0, 24, 14), Get("SpellSheet").rect);
            AddRectangle("", "SpellSheet_Switch_34", new Rectangle(150, 0, 24, 14), Get("SpellSheet").rect);

            //Member 4 selected
            AddRectangle("", "SpellSheet_Switch_41", new Rectangle(94, 0, 24, 14), Get("SpellSheet").rect);
            AddRectangle("", "SpellSheet_Switch_42", new Rectangle(122, 0, 24, 14), Get("SpellSheet").rect);
            AddRectangle("", "SpellSheet_Switch_43", new Rectangle(150, 0, 24, 14), Get("SpellSheet").rect);

            AddRectangle("", "SpellSheet_LO", new Rectangle(5, 18, 26, 22), Get("SpellSheet").rect);
            AddRectangle("", "SpellSheet_UM", new Rectangle(32, 18, 26, 22), Get("SpellSheet").rect);
            AddRectangle("", "SpellSheet_ON", new Rectangle(60, 18, 26, 22), Get("SpellSheet").rect);
            AddRectangle("", "SpellSheet_EE", new Rectangle(88, 18, 26, 22), Get("SpellSheet").rect);
            AddRectangle("", "SpellSheet_PAL", new Rectangle(116, 18, 26, 22), Get("SpellSheet").rect);
            AddRectangle("", "SpellSheet_MON", new Rectangle(144, 18, 26, 22), Get("SpellSheet").rect);

            AddRectangle("", "SpellSheet_BACK", new Rectangle(144, 42, 28, 22), Get("SpellSheet").rect);
            AddRectangle("", "SpellSheet_CAST", new Rectangle(2, 42, 140, 22), Get("SpellSheet").rect);

            AddRectangle("Inventory", "StatsPanel", new Rectangle(206, 104, 232, 201), Get("Inventory").rect);

            AddRectangle("", "Buff_1", new Rectangle(0, 60, 18, 18));
            AddRectangle("", "Buff_2", new Rectangle(20, 60, 18, 18));
            AddRectangle("", "Buff_3", new Rectangle(40, 60, 18, 18));
            AddRectangle("", "Buff_4", new Rectangle(60, 60, 18, 18));
            AddRectangle("", "Buff_5", new Rectangle(80, 60, 18, 18));
            AddRectangle("", "Buff_6", new Rectangle(100, 60, 18, 18));
            AddRectangle("", "Buff_7", new Rectangle(120, 60, 18, 18));

            AddRectangle("Dungeon", "Dungeon", new Rectangle(10, 120, 448*2, 544));

            /// Dungeon_Floor locations
            /// 0  1  2  3  4
            ///[ ][ ][ ][ ][ ]  4
            ///[ ][ ][ ][ ][ ]  3
            ///[ ][ ][ ][ ][ ]  2
            ///   [ ][ ][ ]     1
            ///   [ ][x][ ]     0  
            
            /// 02
            /// Tile u are standing on: 
            /// Height: 38
            /// Width: 225
            AddRectangle("Dungeon", "Dungeon_Floor_02_Left", new Rectangle(10, 282, 225, 38), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_02_Right", new Rectangle(215, 282, 225, 38), Get("Dungeon").rect);

            /// 11 - Coordinates are NOT SET
            AddRectangle("Dungeon", "Dungeon_Floor_11_Far", new Rectangle(-20, 220, 116, 38), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_11_Near", new Rectangle(-60, 258, 133, 30), Get("Dungeon").rect);

            /// 12
            AddRectangle("Dungeon", "Dungeon_Floor_12_Left_Far", new Rectangle(123, 220, 96, 38), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_12_Right_Far", new Rectangle(225, 220, 116, 38), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_12_Left_Near", new Rectangle(90, 258, 133, 30), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_12_Right_Near", new Rectangle(225, 258, 133, 30), Get("Dungeon").rect);

            /// 13
            AddRectangle("Dungeon", "Dungeon_Floor_13_Far", new Rectangle(355, 220, 116, 38), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_13_Near", new Rectangle(373, 258, 133, 30), Get("Dungeon").rect);



            /// 21
            AddRectangle("Dungeon", "Dungeon_Floor_21_Left_Far", new Rectangle(0, 175, 60, 38), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_21_Right_Far", new Rectangle(50, 175, 60, 38), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_21_Left_Near", new Rectangle(-60, 200, 100, 30), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_21_Right_Near", new Rectangle(20, 200, 100, 30), Get("Dungeon").rect);

            /// 22
            AddRectangle("Dungeon", "Dungeon_Floor_22_Left_Far", new Rectangle(140, 175, 60, 38), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_22_Right_Far", new Rectangle(220, 175, 60, 38), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_22_Left_Near", new Rectangle(110, 200, 100, 30), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_22_Right_Near", new Rectangle(190, 200, 100, 30), Get("Dungeon").rect);

            /// 23
            AddRectangle("Dungeon", "Dungeon_Floor_23_Left_Far", new Rectangle(310, 175, 60, 38), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_23_Right_Far", new Rectangle(390, 175, 60, 38), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_23_Left_Near", new Rectangle(330, 200, 100, 30), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_23_Right_Near", new Rectangle(410, 200, 100, 30), Get("Dungeon").rect);

            /// 31
            AddRectangle("Dungeon", "Dungeon_Floor_31_Left_Far", new Rectangle(60, 155, 30, 15), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_31_Right_Far", new Rectangle(120, 155, 30, 15), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_31_Left_Near", new Rectangle(60, 165, 40, 15), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_31_Right_Near", new Rectangle(120, 165, 40, 15), Get("Dungeon").rect);

            /// 32
            AddRectangle("Dungeon", "Dungeon_Floor_32_Left_Far", new Rectangle(155, 155, 30, 15), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_32_Right_Far", new Rectangle(225, 155, 30, 15), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_32_Left_Near", new Rectangle(155, 165, 40, 15), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_32_Right_Near", new Rectangle(225, 165, 40, 15), Get("Dungeon").rect);

            /// 33
            AddRectangle("Dungeon", "Dungeon_Floor_33_Left_Far", new Rectangle(310, 155, 40, 15), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_33_Right_Far", new Rectangle(370, 155, 40, 15), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_33_Left_Near", new Rectangle(310, 165, 30, 15), Get("Dungeon").rect);
            AddRectangle("Dungeon", "Dungeon_Floor_33_Right_Near", new Rectangle(370, 165, 30, 15), Get("Dungeon").rect);
        }


        public static ActiveRectangle Get(string rectangleId)
        {
            if (contentRectangles.Count == 0)
            {
                //do init
                Init();
            }

            if (contentRectangles.ContainsKey(rectangleId))
                return contentRectangles[rectangleId];

            return null;
        }

        public static ActiveRectangle AddRectangle(string screen, string Key, Rectangle rect, Rectangle outerRectangle)
        {
            Rectangle r = new Rectangle(
                rect.X + outerRectangle.X,
                rect.Y + outerRectangle.Y,
                rect.Width,
                rect.Height);

            return AddRectangle(screen, Key, r);
        }

        public static ActiveRectangle AddRectangle(string screen, string Key, Rectangle rect)
        {
            ActiveRectangle ar = new ActiveRectangle();
            ar.rect = rect;
            ar.Name = Key;
            ar.Screen = screen;
            return AddRectangle(Key, ar);

        }

        public static ActiveRectangle AddRectangle(string Key, ActiveRectangle rec)
        {
            contentRectangles.Add(Key, rec);
            return rec;
        }

        public static List<string> IsPointInRectangle(string screen, int X, int Y)
        {
            List<string> rects = new List<string>();

            X -= 16;
            Y -= 16;



            foreach (string s in contentRectangles.Keys)
            {
                ActiveRectangle r = contentRectangles[s];

                if ((r.Screen == screen) || (r.Screen == ""))
                {

                    if ((r.rect.X < X) && ((r.rect.X + r.rect.Width) > X))
                    {
                        if ((r.rect.Y < Y) && ((r.rect.Y + r.rect.Height) > Y))
                        {
                            rects.Add(s);
                        }
                    }
                }
            }

            return rects;
        }

    }
}
